package carson.web.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * 表示用户定义的内容类型，该类型是操作方法的结果
 *
 * @author Carson Chen
 */
public class ContentResult extends AbstractResult {

    /**
     * 实例化ContentResult
     *
     * @param content 内容
     */
    public ContentResult(String content){
        this(content,"text/html");
    }

    /**
     * 实例化ContentResult
     *
     * @param content     内容
     * @param contentType 内容类型
     */
    public ContentResult(String content,String contentType){
        this(content,contentType,"utf-8");
    }

    /**
     * 实例化ContentResult
     *
     * @param content         内容
     * @param contentType     内容类型
     * @param contentEncoding 内容编码
     */
    public ContentResult(String content ,String contentType,String contentEncoding){
        this.content=content;
        this.contentType=contentType;
        this.contentEncoding=contentEncoding;
    }

    private String contentEncoding;

    private String contentType;

    private String content;

    /**
     * 获取内容编码
     *
     * @return 内容编码
     */
    public String getContentEncoding() {
        return contentEncoding;
    }

    /**
     * 设置内容编码
     *
     * @param contentEncoding 内容编码
     */
    public void setContentEncoding(String contentEncoding) {
        this.contentEncoding = contentEncoding;
    }

    /**
     * 获取内容类型
     *
     * @return 内容类型
     */
    public String getContentType() {
        return contentType;
    }

    /**
     * 设置内容类型
     *
     * @param contentType 内容类型
     */
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    /**
     * 获取内容
     *
     * @return 内容
     */
    public String getContent() {
        return content;
    }

    /**
     * 设置内容
     *
     * @param content 内容
     */
    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public void executeResult(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setCharacterEncoding(this.contentEncoding);
            response.setContentType(this.contentType);
            PrintWriter pw = response.getWriter();
            pw.print(this.content);
            pw.flush();
        } catch (IOException e) {

        }
    }
}
